﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class YarnMasterData : BaseFunction
    {


        public YarnMasterData()
            : base()
        {
         
        } 
        public YarnMasterData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public YarnMasterData(OracleConnection _OracleDB, IntexReptServBLClient _proxy)
            : base(_OracleDB, _proxy)
        {

        }
        public bool YarnExist(DataTable dtGSDAT, string YarnArt, string YarnVar)
        {
            try
            {
                string Art = "GSART";
                string Var = "GSVAR";
                Art = "GSCART";
                Var = "GSCVAR";
                if (dtGSDAT.TableName == "GSDAT")
                {
                    Art = "GSART";
                    Var = "GSVAR";
                }
                if (dtGSDAT.TableName == "GASTGA")
                {
                    Art = "GAGART";
                    Var = "GAGVAR";
                }
                    var query = from dwGSDAT in dtGSDAT.AsEnumerable()
                                where dwGSDAT.Field<string>(Art) == YarnArt
                            && dwGSDAT.Field<string>(Var) == YarnVar

                            select dwGSDAT;
                

                int Count = 0;
                foreach (var a in query)
                {
                    Count++;
                }
                if (Count == 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                 Tracer.Error(ex.StackTrace, ex); 
            }
            return true;
        }
        public DataSet GetYarnMaster(DataRow dwGSCOLOR)
        {
            DataSet setResult = new DataSet();
            DataSet setReturn = new DataSet();
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT  * "
                 +" FROM INTEX.GSDAT WHERE GSART = :ArticleNum AND GSVAR=:ArticleVar";
                or1.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwGSCOLOR["GSCART"].ToString();
                or1.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwGSCOLOR["GSCVAR"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "GSDAT";
                }
                setReturn.Merge(setResult); 
            }
            catch (Exception ex)
            {
                 Tracer.Error(ex.StackTrace, ex); 
            }
            return setReturn;
        }
        public DataSet GetYarnColorWay(DataRow dwGSCOLOR)
        {
            DataSet setResult = new DataSet();
            DataSet setReturn = new DataSet();
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * FROM INTEX.GSCOLOR WHERE GSCART = :ArticleNum AND GSCVAR=:ArticleVar";
                or1.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwGSCOLOR["GSCART"].ToString();
                or1.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwGSCOLOR["GSCVAR"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "GSCOLOR";
                }
                setReturn.Merge(setResult);

            }
            catch (Exception ex)
            {
                 Tracer.Error(ex.StackTrace, ex); 
            }
            return setReturn;
        }
        public DataSet GetTechnicalData(DataRow dwGSCOLOR)
        {
            DataSet setResult = new DataSet();
            DataSet setReturn = new DataSet();
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * FROM INTEX.BHSATZ WHERE BHKART = :ArticleNum AND BHKVAR=:ArticleVar";
                or1.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwGSCOLOR["GSCART"].ToString();
                or1.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwGSCOLOR["GSCVAR"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "BHSATZ";
                }
                setReturn.Merge(setResult);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet GetYarnBOM(DataRow dwGSCOLOR)
        {
            DataSet setResult = new DataSet();
            DataSet setReturn = new DataSet();
            try
            {
                OracleCommand or1;
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT*FROM INTEX.GASTGA WHERE GAGART = :ArticleNum AND GAGVAR=:ArticleVar";
                or1.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwGSCOLOR["GSCART"].ToString();
                or1.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwGSCOLOR["GSCVAR"].ToString();
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "GASTGA";
                }
                setReturn.Merge(setResult);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet BuildYarnMasterData(DataRow dwGSCOLOR)
        {
            DataSet setResult = new DataSet();
            DataSet setReturn = new DataSet();
            try
            {
                setResult = GetYarnMaster(dwGSCOLOR);
                setReturn.Merge(setResult);
                setResult = GetYarnColorWay(dwGSCOLOR);
                setReturn.Merge(setResult);
                string YarnArt = dwGSCOLOR["GSCART"].ToString();
                string YarnVar = dwGSCOLOR["GSCVAR"].ToString();
                if (!setReturn.Tables.Contains("GASTGA"))
                {
                    setResult = GetYarnBOM(dwGSCOLOR);
                    setReturn.Merge(setResult);
                }
                if (setReturn.Tables.Contains("GASTGA"))
                {
                    if (!YarnExist(setReturn.Tables["GASTGA"], YarnArt, YarnVar))
                    {
                        setResult = GetYarnBOM(dwGSCOLOR);
                        setReturn.Merge(setResult);
                    }
                }
            }
            catch (Exception ex)
            {
                 Tracer.Error(ex.StackTrace, ex); 
            }
            return setReturn;
        }
    }
}